#include "MenuSystem.h"
#include "IrControl.h"
#include "Menu/Tool/frame.h"
//Bill modify
#include "msgtool.h"
extern		int NavigationInitFlag;
extern		pid_t NavigationPID;
extern		pid_t UIPID;

TFrame		MenuNaviFrame;
extern		unsigned char read_info_data[10];
//unsigned	char navi_data [5] = {0xe0,0x03,0x03,0x04,0xea};
extern  unsigned char send_data_buf[5];
extern		unsigned char navi_status;
extern		unsigned char navi_run;
extern		TRect	  MuteInfoPos;
extern		unsigned char mute_status;
extern		unsigned char rece_handle;
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;
extern		pid_t NavigationPID;
extern		pid_t UIPID;
extern 		unsigned char navi_status;
extern 		int 	Last_ID ;

extern int NavigationInit(void);




int  NaviInfoUpdata (void)
{
	printf ("in navi InfoUpdata !\n");

}

void MenuNaviDrawBackgroup(TCanvas *pCanvas)
{
	//DrawJpeg(pCanvas, 0, 0, "/res_jpeg/menu_navi_start.jpg");//"/res_jpeg/menu_navi.jpg" );
	DrawJpeg(pCanvas, 0, 0, "/res_jpeg/3/0.jpg");
}

void MenuNaviDraw( BOOL bDrawAll )
{
	if( bDrawAll )
		MenuNaviDrawBackgroup(MenuNaviFrame.pCanvas);
/*	if (mute_status == 1)/////////////20080129
	{
		g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_YELLOW, 0, 0 );
		g_Puts( MenuNaviFrame.pCanvas, FONT33, MuteInfoPos, TEXT_ALIGN_CENTER, MuteInfoStr);
	}	
	*/
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	//dt_Present();
	
	printf("after\n");
}

BOOL MenuNaviBtnEnter(void)
{
	//MenuMainInit();	
	printf("CHILD: Goodbye!\n");  ////////////1224 test fork.     
	exit(35);
	return TRUE;
}

//Bill modify
void MenuNaviInit()
{
	Message qbuf;
	printf("in MenuNaviInit!!!!!\n");
	ir_SetCurrentID( IR_MENU_NAVI_INIT );
	frm_Create( &MenuNaviFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );//////////////20080129
	
	if(!NavigationInitFlag)
	{		
			MenuNaviDraw( TRUE );//20080228		
			printf (" ___in NavigationInitFlag =0 \n");
	navi_status = 1;
	navi_run = 1;
	if(init_message_queue() ==  0)
		{
			//return -1;
			printf ("init message queue fail!\n");
		}
	
		while(1)
		{
			NavigationInit();
			
			qbuf.mtype = UIPID;
			read_message(&qbuf, 0);
			if(qbuf.mcommond == MQ_COMMOND_INIT_READY)
				break;
		}
		printf("vofk_test2 PARENT: wait ok!\n");
		NavigationInitFlag = 1;
	}
	navi_status = 1;///20080229 add
	navi_run = 1;

	qbuf.mtype = NavigationPID;
	qbuf.mcommond = MQ_COMMOND_ACTIVE;
	sprintf(qbuf.mtext, "MQ_COMMOND_ACTIVE");
	send_message(&qbuf, 0);
}

/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;
	Message qbuf;
	
	switch (read_info_data[2])
	{
					case	ID_POWER:						
						break;
						
					case	ID_DISPLAY_SCREEN:
							switch (read_info_data[3])
								{
									case 	ID_SCREEN_MENU:
											MenuMainInit();
										break;		
									case 	ID_SCREEN_RADIO:
														
												qbuf.mtype = NavigationPID;
												qbuf.mcommond = MQ_COMMOND_NON_ACTIVE;
												sprintf(qbuf.mtext, "MQ_COMMOND_NON_ACTIVE");
												send_message(&qbuf, 0);	
												navi_status = 0;
												/*if (Last_ID == IR_MENU_RADIO_INIT)
													{	
														dt_Present();
														ir_SetCurrentID( IR_MENU_RADIO_INIT );
													}
												else*/
												{
													MenuRadioInit();
												}													
									 		
									 		//else	
											//MenuRadioInit();
										break;									
									//case	ID_SCREEN_CDLOAD:											
									case	ID_SCREEN_CDDISP:
									case	ID_SCREEN_MP3DISP:
									//case	ID_SCREEN_NODISC:
									//case	ID_SCREEN_DISCERR:
									//case	ID_SCREEN_EJECT:
									/*	
										printf ("navi_status is %d,last_id is %d \n", navi_status,Last_ID);					
												qbuf.mtype = NavigationPID;
												qbuf.mcommond = MQ_COMMOND_NON_ACTIVE;
												sprintf(qbuf.mtext, "MQ_COMMOND_NON_ACTIVE");
												send_message(&qbuf, 0);	
												navi_status = 0;	
												if (Last_ID == IR_MENU_CD_INIT)
													{	
														dt_Present();
														ir_SetCurrentID( IR_MENU_CD_INIT );
													}
													else
									
														{
															MenuCdInit();
														}	
											*/																			
									 		qbuf.mtype = NavigationPID;
												qbuf.mcommond = MQ_COMMOND_NON_ACTIVE;
												sprintf(qbuf.mtext, "MQ_COMMOND_NON_ACTIVE");
												send_message(&qbuf, 0);	
												navi_status = 0;	
												MenuCdInit();
									 		
											rece_handle = 0;
										break;
									case	ID_SCREEN_MEDIEA:
												qbuf.mtype = NavigationPID;
												qbuf.mcommond = MQ_COMMOND_NON_ACTIVE;
												sprintf(qbuf.mtext, "MQ_COMMOND_NON_ACTIVE");
												send_message(&qbuf, 0);	
												navi_status = 0;	
												printf ("Last_ID is %d\n",	Last_ID);													
												/*switch (Last_ID)
												{
													case  IR_MEDIA_PHOTO_INIT:
														MediaPhotoInit();
														break;
													case IR_MEDIA_MUSIC_INIT:
														MediaMusicInit();
														break;
													case IR_MEDIA_MOVIE_INIT:
														MediaMovieInit();
														break;
													default:
														MenuMediaInit();
													
												}	
												*/
													MenuMediaInit();										
											//MenuMediaInit();
											rece_handle = 0;
										break;
									
									case 	ID_SCREEN_NAVI:
										//MenuNaviInit();//20080229
										break;	
										
									case	 	ID_SCREEN_AUDIODISP:
											MenuAudioInit();
											break;	
									case		ID_SCREEN_VIDEODISP:
										MenuVideoInit();
										break;	
									case		ID_SCREEN_EQDISP:
											AudioEQInit();
										break;	
									case	 	ID_SCREEN_BASTRA:
											AudioBASInit();
										break;	
									case		ID_SCREEN_FADBAL:	
											AudioFADInit();							
										break;	
									}
									rc = 0;//if handled, clear this flag.	
										read_info_data[0] = 0;			
																									
						break;
						
					case	ID_KEY_STATUS:						
						break;
						
					case	ID_SCREEN_INFO:
						switch (read_info_data[3])
						{
							case ID_SCREEN_INFO_CUR_FRQ:
								MenuRadioInit();
								break;
							case ID_SCREEN_INFO_TOTTRK:
							case ID_SCREEN_INFO_CURFLOD:
							case ID_SCREEN_INFO_TOTFLOD:									
							case ID_SCREEN_INFO_CURTRK:
							case ID_SCREEN_INFO_CURTIME:
								MenuCdInit();
								break;
							case ID_SCREEN_INFO_VOLUME:
								//VolumeInit();
								break;
						}

								break;
						
					case	ID_KEYPAD:	
							switch (read_info_data[3])
							{
									case 	ID_MUTE:										
									case	ID_DMUTE:	
										//MenuNaviDraw( TRUE );
										break;									
									case 	ID_SEEKUP:
									case 	ID_SEEKDN:
										break;
									case 	ID_DEST:
									case 	ID_NAVI:
										MenuNaviInit();
										break;
									case 	ID_FMAM:
										//band command;
										break;
									case 	ID_CD:
										MenuCdInit();
										break;				
									case 	ID_MEDIA:
										MenuMediaInit();
										break;
									case 	ID_MENU:
										MenuMainInit();
										break;
										
									case	ID_TVAUX:
										
										break;
							}
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;								 
						break;
						case 	ID_BAND_SEL:
							//MenuRadioInit();//20080227
							rc = 0;//if handled, clear this flag.	
										read_info_data[0] = 0;					
						break;	
						
					default:	
							rc = -1;						
						}
	return 0;					
}

/*
static int send_data_out ( void)
{
	if (dev_SPIWrite(navi_data,5)==0)
		return 0;//hangsheng 071126		
	return -1;
}
*/

static int send_data (void)
{
			
		/*send_data_buf[0] =  HEAD;		
		send_data_buf[1] =  LENGTH;		
		send_data_buf [2] = IDDATA_NAVI;
		send_data_buf [3] = ;
		return ( send_data_out(send_data_buf,ALL_LENGTH) );
		*/
		
}



void MenuNaviInput(UInt32 key)
{
	int flag;
	
	if ( key == IRC_READ_INFO)
	{
		flag =message_dispatch ();
		if (flag == -1)
			printf ("main menu handle receive data error!\n");
			
	}
	
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				//if ( send_data_out())///////1227 masked.because have navi source.
				//	printf ("write failure !\n");	
				//navi_run = 0;
			}
		}
		else
		{
			//MenuNaviBtnEnter();
		}
		LastKey = key;//hangsheng add 071126
	}
}


